<?php

namespace App\Kuafu\Export\Service\ExportFactory;

use App\Kuafu\Export\Model\ExportDownloadModel;
use App\Kuafu\Mybank\Service\MybankService;
use App\Kuafu\Statistics\Service\StatisticsService;
use Illuminate\Support\Str;

/**
 * app-网商钱包记录下载
 */
class ExportMybankWalletRecordService extends BaseExportService
{
    private string $localPath = '';

    public function query(): self
    {
        $params     = $this->params['otherParams'] ?? [];
        $startDate  = (string)($params['startDate'] ?? '');
        $endDate    = (string)($params['endDate'] ?? '');
        $shopIds    = (array)($params['shopIds'] ?? []);
        $type       = (array)($params['type'] ?? []);
        $pageSize   = (int)($params['pageSize'] ?? 0);
        $pageNum    = (int)($params['pageNum'] ?? 0);
        $merchantId = (string)($params['merchantId'] ?? '');
        $export     = (int)($params['export'] ?? 0);

        $dataArr = MybankService::billList($merchantId, $startDate, $endDate, $shopIds, $type, $pageSize, $pageNum,
            $export)['list'];

        $this->localPath = sys_get_temp_dir() . '/' . Str::random();
        self::streamExport($dataArr, 0, $startDate, $endDate, $this->localPath);

        return $this;
    }

    public function path(): string
    {
        return $this->upload($this->localPath);
    }


    public static function streamExport(
        array $dataArr,
        int $shopId,
        string $startAt,
        string $endAt,
        string $path
    ): array {

        $data = self::getToChange($dataArr);

        // 生成excel
        $fileType = ExportDownloadModel::FILE_TYPE_APP_MYBANK_WALLET_RECORD;
        $client   = ExportDownloadModel::CLIENT_TYPE_APP;
        $fileName = (new ExportDownloadModel())->buildFileName($shopId, strtotime($startAt), strtotime($endAt),
            $fileType, $client);

        StatisticsService::spreadsheetExcel([
            [
                'title' => $data['title'],
                'info'  => [],
                'rows'  => iterator_to_array($data['rows']),// 把yield转成数组
            ]
        ], $fileName, $path);

        return $data;
    }

    /**
     * 导出信息组装
     * @param $list
     * @return array
     */
    public static function getToChange($list): array
    {
        return [
            'title' => '网商钱包明细',
            'info'  => [],
            'rows'  => self::yieldOrderInfo($list)
        ];
    }

    public static function yieldOrderInfo($list): \Generator
    {
        $rows = [
            '时间',
            '门店',
            '交易类型',
            '交易金额',
            '交易后余额',
            '操作单号',
        ];
        yield $rows;
        foreach ($list as $key => $value) {
            $shopName = '';
            if ($value['shopId']) {
                $shopName = $value['shopCode'] . $value['shopName'];
            }
            $rows[$key + 1] = [
                $value['transDate'],
                $shopName,
                $value['transDesc'],
                $value['transAmount'],
                $value['afterRemainingAmount'],
                $value['transId'] . ' ',
            ];
            yield $rows[$key + 1];
        }
    }
}
